package com.larissa.quiz;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.MenuProvider;
import androidx.lifecycle.ViewModelProvider;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,MenuProvider{
    private static final String TAG="MainActivity";
    private static final String KEY_ANSWER="answer";
    private static final String KEY_RESULT="result";
    private TextView questionTextView;
    private View btnTrue;
    private View btnFalse;
    private int screenOrientation;
    private QuizViewModel viewmodel;
    private boolean hasClicked=false;
    private ActivityResultLauncher<Intent> helpLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG,"onCreate() called");

        viewmodel=new ViewModelProvider(this).get(QuizViewModel.class);
        Log.d(TAG,String.format("Got a QuizViewModel:%s",viewmodel));

        addMenuProvider(this);
        helpLauncher=registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result->{
            if(result.getResultCode()== Activity.RESULT_OK){
                hasClicked=result.getData().getBooleanExtra(KEY_RESULT,false);
            }
            Log.d(TAG,String.format("callback() called and result:%s",result));
        });

        questionTextView=(TextView) findViewById(R.id.tx_question);
        screenOrientation=getResources().getConfiguration().orientation;
        if(screenOrientation== Configuration.ORIENTATION_PORTRAIT){
            btnTrue=findViewById(R.id.btn_true);
            btnFalse=findViewById(R.id.btn_false);
        }
        else if(screenOrientation== Configuration.ORIENTATION_LANDSCAPE){
            btnTrue=findViewById(R.id.btn_check);
            btnFalse=findViewById(R.id.btn_clear);
        }

        btnTrue.setOnClickListener(MainActivity.this);
        btnFalse.setOnClickListener(this);

        showQuestion();
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu){
//        getMenuInflater().inflate(R.menu.activity_main,menu);
//        return true;
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item){
//        switch (item.getItemId()){
//            case R.id.btn_help:
//                Intent intent=new Intent(this,HelpActivity.class);
//                intent.putExtra(KEY_ANSWER,viewmodel.isAnswer());
//                startActivity(intent);
//                return true;
//            default:
//                return true;
//        }
//    }

    private void updateQuestion(View view){
        if(!hasClicked){
            if(view.getId()==R.id.btn_true || view.getId()==R.id.btn_check){
                if(viewmodel.isAnswer()) viewmodel.correctCount++;
            }
            else if(view.getId()==R.id.btn_false || view.getId()==R.id.btn_clear){
                if(!viewmodel.isAnswer()) viewmodel.correctCount++;
            }
        }
        else{
            Toast.makeText(this,"This question is invalid",Toast.LENGTH_SHORT).show();
            hasClicked=false;
        }
        viewmodel.moveToNext();
        showQuestion();

    }

    private void showQuestion() {
        if(viewmodel.currentIdx==-1){
            btnTrue.setEnabled(false);
            btnFalse.setEnabled(false);
            questionTextView.setText(viewmodel.getQuestionText());
            Toast.makeText(this,String.format("%s questions are right",viewmodel.correctCount),Toast.LENGTH_LONG).show();
        }
        else{
            if(viewmodel.getQuestionText().equals("")){
                btnTrue.setEnabled(false);
                btnFalse.setEnabled(false);
            }else{
                questionTextView.setText(viewmodel.getQuestionText());
            }
        }
    }

    @Override
    public void onClick(View view) {
        updateQuestion(view);
    }

    @Override
    public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
        menuInflater.inflate(R.menu.activity_main,menu);
    }

    @Override
    public boolean onMenuItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()){
            case R.id.btn_help:
                Intent intent=new Intent(this,HelpActivity.class);
                intent.putExtra(KEY_ANSWER,viewmodel.isAnswer());
//                startActivity(intent);
                helpLauncher.launch(intent);
                return true;
            case R.id.btn_all_questions:
                Intent intent1=new Intent(this,QuestionActivity.class);
                startActivity(intent1);
                return true;
            case R.id.btn_add_question:
                Intent intent2=new Intent(this,CreateQuestionActivity.class);
                startActivity(intent2);
                return true;
            default:
                return true;
        }
    }

    @Override
    public void onStart(){
        super.onStart();
        Log.d(TAG,"onStart() called");
    }

    @Override
    public void onResume(){
        super.onResume();
        Log.d(TAG,"onResume() called");
    }

    @Override
    public void onPause(){
        super.onPause();
        Log.d(TAG,"onPause() called");
    }

    @Override
    public void onStop(){
        super.onStop();
        Log.d(TAG,"onStop() called");
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d(TAG,"onDestroy() called");
    }


}